const {ShortMsgModel} = require('../../models/index')
const sendMsg  = require("../../commons/send_msg.js")
const koaTool = require('../../commons/koa_tool')


class ShortMsgController {
    create = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['templateName', 'appId', 'signName', 'templateId'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let newData = Object.assign({}, input, {
            
        })
        let detail = await ShortMsgModel.create(newData)
        return koaTool.success(ctx,'创建成功');
    }

    detail = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        const detail = await ShortMsgModel.findById(input.id)
        koaTool.succ(ctx, {
            detail
        })
    }

  //更新
  update = async (ctx) => {
    console.log('短信模板更新')
    let [checkError, input] = koaTool.checkInput(ctx, [])
    if (checkError) {
        return koaTool.fail(ctx, checkError)
    }

    const {isNull} = require("../../commons/util.js");
    if(isNull(input.id)){
        return koaTool.error(ctx,"异常");
    }

    let detail = await ShortMsgModel.findById(input.id)
    if (!detail) {
        return koaTool.error(ctx,"短信模板异常");
    }
    const {
        templateName,         //短信模板名称
        appId,         
        signName,      //短信模板签名
        templateId,    //短信模板id
        hasIncludeParams,
    } = input;


    detail.hasIncludeParams = hasIncludeParams;

    if (!isNull(templateName)) {
        detail.templateName = templateName
    }
    

    if (appId) {
        detail.appId = appId
    }

    if (signName) {
        detail.signName = signName
    }

    if (templateId) {
        detail.templateId = templateId
    }

    await detail.save()
    return koaTool.success(ctx, '编辑成功');
}

    delete = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        await ShortMsgModel.findByIdAndRemove(input.id)
        koaTool.succ(ctx, {
            msg: '成功'
        })
    }

    list = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, [])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        let query = {
        };
        let total = await ShortMsgModel.countDocuments(query)
        let list = await ShortMsgModel.find(query).sort([['_id', -1]])
        koaTool.succ(ctx, {
            list,
            total
        })
    }

    sendmsg = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, [])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let { phone, shortMsgId } = input;
        let shortMsg = await ShortMsgModel.findById(shortMsgId).lean();
        if(!shortMsg){
            return koaTool.error(ctx,'数据不存在');
        }

        let parmas = {
            PhoneNumberSet: [(phone || "18816849606")],
            SmsSdkAppId: shortMsg.appId,
            SignName: shortMsg.signName,
            TemplateId: shortMsg.templateId,
            TemplateParamSet: ['6'],
        }
        console.log(parmas)
        let sendRes = await sendMsg(parmas)
        console.log("sendRes",sendRes)
        // let detail = await ShortMsgModel.create(newData)
        return koaTool.success(ctx,'发送短信-ing');
    }


    getDict = async (ctx) => {
        let list = await ShortMsgModel.find({}).sort([['_id templateName', -1]]).lean();

        koaTool.succ(ctx, {
            list,
        })
    }
 
}

module.exports = new ShortMsgController()
